package com.dataaccess.dataaccess.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.model.DeviceSwitch;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.ByteUtil;
import com.dataaccess.dataaccess.web.mapper.DeviceSwitchMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: YangYu
 * @ClassName: DeviceSwitchService
 * @Date: Created in 9:17 2023/2/20
 * @Description: 设备开关量服务
 */
@Service
public class DeviceSwitchService extends BaseService {

    @Resource
    private DeviceSwitchMapper deviceSwitchMapper;
    @Resource
    private AsyncService asyncService;

    /**
     * @Author YangYu
     * @Description 开关状态更新
     * @Date Created in 9:19 2023/2/20
     * @Param [jsonParam]
     */

    public RespEntity deviceSwitchStateUpdate(JSONObject jsonParam) {

        //原始数据 9c9c07550100011704080f093801020c
        String code = jsonParam.getStr("code");
        if (code.length() != 32){
            return RespEntity.fail("数据格式错误");
        }
        //传输装置编码
        String transDeviceCode = code.substring(4,14);
        jsonParam.put("number",transDeviceCode);
        jsonParam.put("state","1");
        //异步更新传输装置状态
        asyncService.updateDeviceCurrentState(jsonParam);
        //寄存器值(转为二进制)
        String codeValue = ByteUtil.hexToBinaryString(code.substring(28,30));
        //报警标识
        boolean flag = getFlag(codeValue,transDeviceCode);
        if (flag){
            //批量更新数据
            List<Map<String,Object>> list = batchUpdateData(codeValue,transDeviceCode);
            deviceSwitchMapper.batchUpdate(list);
            JSONObject jsonParamN = new JSONObject();
            jsonParamN.put("number", transDeviceCode);
            jsonParamN.put("interfaceType", "1");
            jsonParamN.put("content", "开关状态变化");
            jsonParamN.put("alarmTypeName", "开关状态报警");
            jsonParamN.put("alarmtime", DateUtil.now());
            jsonParamN.put("deviceClassify", "19");
            fireFightingiot(jsonParamN, "communication", "notificationAlarmHandel", "alarmInfo");
            jsonParam.put("state","2");
            //异步更新传输装置状态
            asyncService.updateDeviceCurrentState(jsonParam);
        }
        return RespEntity.success();
    }
    public boolean getFlag(String codeValue,String transDeviceCode ){
        //开关列表
        List<DeviceSwitch> deviceSwitchList = deviceSwitchMapper.deviceSwitchList(transDeviceCode);
        //标识
        boolean flag = false;
        for (DeviceSwitch deviceSwitch : deviceSwitchList){
            if (deviceSwitch.getNumber().equals("1")){
                if (codeValue.charAt(7) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }else if (deviceSwitch.getNumber().equals("2")){
                if (codeValue.charAt(6) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }else if (deviceSwitch.getNumber().equals("3")){
                if (codeValue.charAt(5) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }else if (deviceSwitch.getNumber().equals("4")){
                if (codeValue.charAt(4) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }else if (deviceSwitch.getNumber().equals("5")){
                if (codeValue.charAt(3) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }else if (deviceSwitch.getNumber().equals("6")){
                if (codeValue.charAt(2) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }else if (deviceSwitch.getNumber().equals("7")){
                if (codeValue.charAt(1) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }else if (deviceSwitch.getNumber().equals("8")){
                if (codeValue.charAt(0) == '1'){
                    if (!deviceSwitch.getCurrentstate().equals("1")){
                        flag = true;
                        break;
                    }
                }else {
                    if (!deviceSwitch.getCurrentstate().equals("2")){
                        flag = true;
                        break;
                    }
                }
            }

        }
        return flag;
    }
    public static List<Map<String,Object>> batchUpdateData(String codeValue,String transDeviceCode){
        //batchUpdate
        //批量更新数据
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        if (codeValue.charAt(7) == '1'){
            map = new HashMap<>();
            map.put("number","1");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","1");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }

        if (codeValue.charAt(6) == '1'){
            map = new HashMap<>();
            map.put("number","2");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","2");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }
        if (codeValue.charAt(5) == '1'){
            map = new HashMap<>();
            map.put("number","3");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","3");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }
        if (codeValue.charAt(4) == '1'){
            map = new HashMap<>();
            map.put("number","4");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","4");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }
        if (codeValue.charAt(3) == '1'){
            map = new HashMap<>();
            map.put("number","5");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","5");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }
        if (codeValue.charAt(2) == '1'){
            map = new HashMap<>();
            map.put("number","6");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","6");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }
        if (codeValue.charAt(1) == '1'){
            map = new HashMap<>();
            map.put("number","7");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","7");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }
        if (codeValue.charAt(0) == '1'){
            map = new HashMap<>();
            map.put("number","8");
            map.put("currentstate","1");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }else {
            map = new HashMap<>();
            map.put("number","8");
            map.put("currentstate","2");
            map.put("transDeviceCode",transDeviceCode);
            list.add(map);
        }
        return list;
    }

}
